import Vue from 'vue'
import VueRouter from 'vue-router'
import HomeView from '../views/HomeView.vue'
import jwt_decode from 'jwt-decode'
Vue.use(VueRouter)

const routes = [{
    path: '/',
    name: 'home',
    component: HomeView,
    children: [{
      path: '/index',
      name: 'index',
      component: () => import( /* webpackChunkName: "about" */ '../views/index.vue')
    }, {
      path: '/welcome',
      name: 'welcome',
      component: () => import( /* webpackChunkName: "about" */ '../views/welcome.vue')
    }, {
      path: '/info',
      name: 'info',
      component: () => import( /* webpackChunkName: "about" */ '../views/info.vue')
    }, ]
  },
  {
    path: '/about',
    name: 'about',
    component: () => import( /* webpackChunkName: "about" */ '../views/AboutView.vue')
  },
  {
    path: '/login',
    name: 'login',
    component: () => import( /* webpackChunkName: "about" */ '../views/Login.vue')
  },
  {
    path: '/regist',
    name: 'regist',
    component: () => import( /* webpackChunkName: "about" */ '../views/Regist.vue')
  }
]
const router = new VueRouter({
  routes
})
// router.afterEach((to, from, next) => {
//   if (to.path == '/login' || to.path == '/regist')  next()
//   //查看token
//   let token = localStorage.getItem('token')

//   let result = jwt_decode(token)
//   console.log(result,"99999999");
//   let nowTime = parseInt(new Date().getItem() / 1000)
//   if (token) {
//     if (nowTime - result.exp > 0) {
//       next('/login')
//     } else {
//       next()
//     }
//   } else {
//     next('/login')
//   }
// })

// 全局守卫
// router.beforeEach((to,from,next)=>{
// console.log(to);
// console.log(from);
//    next()

router.beforeEach((to, from, next) => {
  if (localStorage.getItem('token')) {
    next()
  } else if (to.path == '/login' || to.path == '/regist') {
    next()
  } else {
    let token = localStorage.getItem('token')
    let result = jwt_decode(token)
    console.log(result, "99999999");
    let nowTime = parseInt(new Date().getItem() / 1000)
    if (token) {
      if (nowTime - result.exp > 0) {
        next('/login')
      } else {
        next()
      }
    } else {
      next('/login')
    }
  }
})
export default router